package com.itheima.demo.dao;

import com.itheima.demo.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


@Component
public class UserDao {
    @Autowired
    private RestTemplate restTemplate;

    Logger logger = LoggerFactory.getLogger(UserDao.class);
    @HystrixCommand(fallbackMethod = "queryUserByIdFallBack")
    public User queryUserById(Long id) {
        long start = System.currentTimeMillis();
        User us = this.restTemplate.getForObject("http://user-service:/user/" + id, User.class);
        long endtime = System.currentTimeMillis();

        logger.info("访问用时：{}", endtime - start);
        return us;
    }

    public User queryUserByIdFallBack(Long id) {

        User user = new User();
        user.setId(id);
        user.setName("用户信息查询出现异常！");
        return user;

    }
}
